﻿Imports System.Data
Imports System.Data.OleDb
Imports validaciones.metodos

Public Class Consultas

    Public validar As New validaciones.metodos
    Public conexion As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0; Data Source=nuevozoo.accdb")

    Private Sub cargarConsultas()

        'CONSULTAS---------------------------------------------------------------------

        'para consultas
        Dim adaptador As New OleDbDataAdapter("select animales.nombre, consultas.cod_consulta, consultas.cod_animal, consultas.fecha, consultas.motivo, consultas.precio, consultas.hora from consultas, animales where animales.cod_animal=consultas.cod_animal order by animales.nombre", conexion)
        Dim miDataset As New DataSet

        'limpiar DataBlindings para que no de error 
        text_codcon.DataBindings.Clear()
        text_codan.DataBindings.Clear()
        DateTimePicker1.DataBindings.Clear()
        Text_motivo.DataBindings.Clear()
        Text_precio.DataBindings.Clear()
        DateTimePicker2.DataBindings.Clear()

        'rellenar datagriview
        adaptador.Fill(miDataset, "consultas")
        DataGridViewCon.DataSource = miDataset
        DataGridViewCon.DataMember = "consultas"

        'cambiar tamaño de las columnas
        DataGridViewCon.Columns("nombre").Width = 70
        DataGridViewCon.Columns("hora").Width = 70

        'ocultar columna de codigo
        DataGridViewCon.Columns("precio").Visible = False
        DataGridViewCon.Columns("cod_consulta").Visible = False
        DataGridViewCon.Columns("motivo").Visible = False
        DataGridViewCon.Columns("cod_animal").Visible = False

        'rellenar textbox
        text_codcon.DataBindings.Add("text", miDataset, "consultas.cod_consulta")
        text_codan.DataBindings.Add("text", miDataset, "consultas.cod_animal")
        Text_motivo.DataBindings.Add("text", miDataset, "consultas.motivo")
        Text_precio.DataBindings.Add("text", miDataset, "consultas.precio")
        DateTimePicker1.DataBindings.Add("text", miDataset, "consultas.fecha")
        DateTimePicker2.DataBindings.Add("text", miDataset, "consultas.hora")
    End Sub

    Private Sub Consultas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarConsultas()
    End Sub

    Private Sub b_eliminarcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_eliminarcon.Click
        'borrar consulta seleccionada
        Dim res As Integer
        res = MsgBox("¿Está seguro de borrar la cita?", MsgBoxStyle.YesNo, "Aviso")
        If res = vbYes Then
            conexion.Open()
            Dim myCommand As OleDbCommand
            myCommand = New OleDbCommand("delete from consultas where cod_consulta = " & text_codcon.Text, conexion)
            myCommand.ExecuteNonQuery()
            conexion.Close()
            cargarConsultas()
        End If
    End Sub

    Private Sub b_modificarcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_modificarcon.Click

        Try
            'Con estas variables creo la hora actual, fecha actual, hora seleccionada y fecha seleccionada
            'y compare el lapso de tiempo entre ambas, si es menor que cero en fecha o en hora siendo la fecha actual
            'saltará una excepcion
            Dim hora_selecc As Date = DateTimePicker2.Value.Hour & ":" & DateTimePicker2.Value.Minute
            Dim hora_acc As Date = Date.Now.Hour & ":" & Date.Now.Minute
            Dim fecha As Long = DateDiff(DateInterval.Day, Date.Now, DateTimePicker1.Value)
            Dim hora As Long = DateDiff(DateInterval.Minute, hora_acc, hora_selecc)

            If fecha < 0 Then

                Throw New ExcepcionFecha
            ElseIf fecha = 0 And hora < 0 Then
                Throw New ExcepcionFecha

            Else
                'modificar la cita seleccionada
                Dim res As Integer

                If DateTimePicker1.Text <> "" And Text_precio.Text <> "" And DateTimePicker2.Text <> "" Then
                    If validar.numeros(Text_precio.Text) Then
                        res = MsgBox("¿Está seguro de modificar la cita?", MsgBoxStyle.YesNo, "Aviso")
                        If res = vbYes Then
                            conexion.Open()
                            Dim myCommand As OleDbCommand
                            myCommand = New OleDbCommand("update consultas set fecha ='" & DateTimePicker1.Text & "', hora = '" & DateTimePicker2.Text & "', motivo= '" & Text_motivo.Text & "', precio = '" & Text_precio.Text & "' where cod_consulta = " & text_codcon.Text, conexion)
                            myCommand.ExecuteNonQuery()
                            conexion.Close()
                        End If
                    Else
                        MsgBox("el precio tiene un mal formato", , "Error")
                    End If

                Else
                    MsgBox("Sólo el campo de motivo puede estar vacío", , "Error")
                End If
                cargarConsultas()
            End If
         Catch ex2 As ExcepcionFecha
            MsgBox("La fecha indicada ya ha pasado", , "Error")

        Catch ex As Exception
            MsgBox(ex.Message, , "Error")

        End Try
    End Sub


    Private Sub Text_precio_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Text_precio.Validating
        If Not IsNumeric(Text_precio.Text) Then
            Text_precio.Text = 0
        End If
    End Sub

    Private Sub VolverToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VolverToolStripMenuItem.Click
        GestionGeneral.Show()
        Me.Close()
    End Sub
End Class